package org.ttitfly.zuoyue.dao.room;


import java.util.ArrayList;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Component;
import org.springside.modules.orm.Page;
import org.springside.modules.orm.hibernate.HibernateDao;
import org.ttitfly.zuoyue.entity.room.RoomProject;
@Component
public class RoomProjectDao extends HibernateDao<RoomProject, Long> {
	
	
	public RoomProject getByProcessInstanceId(int processInstanceId) {
		return this.findUniqueBy("processInstanceId", processInstanceId);
	}

	public void update(RoomProject roomProject) {
		save(roomProject);
	}

	public Page<RoomProject> find() {
		return null;
	}

	public boolean deleteRelation(Long attachmentId) {
		Query query = getSession().createSQLQuery("DELETE FROM ROOM_PROJECT_ATTACHMENT WHERE ATTACHMENT_ID = ?");
		int num =query.setLong(0, attachmentId).executeUpdate();
		if(num > 0){
			return true;
		}else{
			return false;
		}
	}
	
	public boolean removeProject(int processInstanceId){
		StringBuilder hql = new StringBuilder("delete from room_project where process_instance_id = ?");
		Query query = getSession().createSQLQuery(hql.toString());
		query.setInteger(0, processInstanceId);
		int num = query.executeUpdate();
		if(num > 0){
			return true;
		}else{
			return false;
		}
	}
	
	public int getRoomProject(String name){
		RoomProject rp=null;
		Query query = getSession().createQuery("from RoomProject rp where rp.name=:name");
		query.setParameter("name", name);
		rp=(RoomProject)query.uniqueResult();
		if(rp!=null){
			return 1;
		}
		return 0;
	}

	
	/**
	 * 
	 * @param areaId (区域编号不能为空,如果没有区域限制可以设置为0)
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<RoomProject> getAllRoomProject(String areaId){
		StringBuilder sb=new StringBuilder("select * from Room_Project where 1=1");
		List<Object> params = new ArrayList<Object>();
		if(null!=areaId && areaId.length()>0){
			String[] areas=areaId.split(",");
			sb.append(" and area_id in (");
			for(String area : areas){
				sb.append("?,");
				params.add(area);
			}
			sb.deleteCharAt(sb.length() - 1);
			sb.append(" ) ");
		}
		Query query = getSession().createSQLQuery(sb.toString()).addEntity(RoomProject.class);
		int i=0;
		for(Object param :  params){
			query.setParameter(i++, param);
		}
		
		return query.list();
	}

	
	public RoomProject findRoomProject(int processInstanceId){
//		RoomProject roomProject=null;
//		Query query = getSession().createQuery("from RoomProject roomProject where roomProject.processInstanceId=?");
//		query.setInteger(0, processInstanceId);
//		roomProject = (RoomProject) query.uniqueResult();
//		return roomProject;
		
		return getByProcessInstanceId(processInstanceId);
	}

	public RoomProject findRoomProjectById(Long id){
		return this.get(id);
	}
	
	@SuppressWarnings("unchecked")
	public List<RoomProject> getRoomProjectByProIds(List<Integer> processInstanceIds){
		List<RoomProject> roomProjectList = new ArrayList<RoomProject>();
		Criteria criteria = this.getSession().createCriteria(RoomProject.class.getName());
		criteria.add(Restrictions.in("processInstanceId", processInstanceIds));
		List list = criteria.list();
		if(list != null && list.size() >0){
			roomProjectList = list;
		}
		return roomProjectList;
	}
}
